<?php
/*
Template Name: WPSPO modèle Profil Participant
*/

// Si on est pas logué => rien à faire ici => on le renvoi
/*if( !is_user_logged_in() ) {
    wp_redirect( get_option( 'siteurl').'/wp-login.php' );
    exit();
} */

$erreurPage = 0;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Si un participant est recherché
if( isset($_GET["profil"]) ) {

    $args  = array( 
        'role' => 'participant', 
        'search' => $_GET["profil"], 
        'search_columns' => array( 'ID', 'user_nicename', 'display_name' ) 
    );
    
} elseif( isset($_POST['submit-search']) && isset($_POST['search']) && isset($_POST['search-verif'])) {
    
        if ( wp_verify_nonce($_POST['search-verif'], 'go-search') ) {

            $args  = array( 
                'role' => 'participant', 
                'search' => $_POST['search'], 
                'search_columns' => array( 'ID', 'login', 'user_email', 'user_nicename', 'display_name' ),
                'paged' => $paged,
            );
            
        }
    
} else {
    
    //wp_redirect( get_option( 'siteurl') );
    //exit();

    $args  = array(
    // search only for Authors role
    'role' => 'participant',
    // order results by display_name
    'orderby' => 'ID',
    'order' => 'DESC',
    'paged' => $paged,
    );
    
}
//print_r($args);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
// Get the results
$authors = $wp_user_query->get_results();

get_header();


?>
          
<div class="content">
    

    <article <?php if( !empty( $rv['review'] ) ) echo $rv['review']; post_class('post-listing'); ?>>
        <div class="post-inner">
            <header class='page-header'>
                <div>
                    <h1 class="name post-title entry-title entry-title"><?php _e('Search', 'sponsorpress'); ?></h1>
                    <div style="margin-top:10px;margin-bottom:10px;">
                        <form method="post" action="<?php echo get_permalink(WPSPO_ID_PARTICIPANTS); ?>#">
                            <?php wp_nonce_field('go-search', 'search-verif'); ?>
                            <input type="text" name="search" id="search" value="" />
                            <input type="submit" name="submit-search" value="<?php _e('Submit', 'sponsorpress'); ?>" />
                        </form>
                    </div>
                    <div class="clear"></div>
                </div>
            </header>
        </div>
    </article>
    <div class="clear epsace10">&nbsp;</div>
    
<?php

// Check for results
if (!empty($authors)) {

    // loop trough each author
    foreach ($authors as $author) {
        // get all the user's data
        $author_info = get_userdata($author->ID);
        //echo '<li>'.$author_info->first_name.' '.$author_info->last_name.'</li>';
        $age = wspo_affiche_age($author->ID);
        
        //print_r($author);
        ?>
        <article <?php if( !empty( $rv['review'] ) ) echo $rv['review']; post_class('post-listing'); ?>>
            <div class="post-inner">
                <header class='page-header'>
                    <div>
                        <div style="float:left;width:25%;"><?php echo get_wpspo_user_avatar($author->ID); ?></div>
                        <div style="float:left;width:75%">
                            <h1 class="name post-title entry-title entry-title"><?php echo $author_info->display_name ?><?php if($age) { echo ' <span style="font-size:14px;">('.$age.' ans)</span>'; } ?></h1>
                            <div style="margin-top:10px;margin-bottom:10px;"><?php echo (nl2br($author_info->description)); ?></div>
                        </div>
                        <div>
                            <?php
                            $listParticipant = WPSPO_Users::get_listByUser($author->ID);
                            //print_r($listParticipant);
                            if( $listParticipant ) {
                                echo 'Participe à ces défis :<br />';
                                foreach ( $listParticipant as $participant ) {
                                    $leDefi = get_post($participant->users_id_defi);
                                    $data = WPSPO_defis::get_options($participant->users_id_defi);
                                    $valueOptions = json_decode($data->defis_option_value);
                                    echo '<a href="'.get_permalink($leDefi->ID).'"><strong>'.$leDefi->post_title.' ('.$valueOptions->ville_defis.')</strong></a><br />';
                                }
                            }
                            ?>
                        </div>
                        <div class="clear"></div>
                    </div>
                </header>
            </div>
        </article>
    
        <div class="clear epsace10">&nbsp;</div>

<?php

    }

    wpspo_theme_pagination();
        
} else {
    
?>
    <article <?php if( !empty( $rv['review'] ) ) echo $rv['review']; post_class('post-listing'); ?>>
             <div class="post-inner">
                <h1 class="name post-title entry-title entry-title">
                    <?php
                        if($_GET["profil"]) {
                            printf('No result for this participant : %s', $_GET["profil"], 'sponsorpress'); 
                        } elseif($_POST['search']) {
                            printf('No result for search : %s', $_POST['search'], 'sponsorpress'); 
                        } else {
                            _e('No participant found', 'sponsorpress'); 
                        }
                    ?>
                 </h1>
             </div>
    </article>
    <div class="clear epsace10">&nbsp;</div>
    <?php
}
    ?>
</div>  
<?php get_sidebar(); ?>
<?php get_footer(); ?>

